/**
 * 
 */
package sqs.service;

import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;

import sqs.ejb.sessionbean.QuestionnaireMgr;
import sqs.ejb.sessionbean.UserMgr;
import sqs.model.MCQQuestion;
import sqs.model.Option;
import sqs.model.Question;
import sqs.model.QuestionFactory;
import sqs.model.Questionnaire;
import sqs.model.Scale;
import sqs.model.ScaleOption;
import sqs.model.User;


/**
 * @author kjleng
 *
 */
public class TestCreateQuestionnaire {
	Questionnaire qnaire;
	
	public void createQuestionnaire() throws Exception {
		InitialContext ctx = new InitialContext();
		QuestionnaireMgr mgr = (QuestionnaireMgr)ctx.lookup("ejb/QuestionnaireJNDI");
		UserMgr userMgr = (UserMgr)ctx.lookup("ejb/UserJNDI");
		User user = userMgr.getUserByUserName("kjleng");
		
		qnaire = new Questionnaire();
		qnaire.setActive(Questionnaire.ACTIVE);
		qnaire.setDescription("description for questionnaire 1");
		qnaire.setQuestionnaireTitle("questionnaire title for 1");
		qnaire.setOwner(user);
		qnaire.setQuestionsPerPage(10);
		
		List<Question> qns = new ArrayList<Question>();
		qnaire.setQuestions(qns);
		
		// Q1: create MultipleAnsMCQ
		Question q1 = createMultipleAnsMCQ();
		qns.add(q1);
		
		// Q2: create RatingQuestion
		Question q2 = createRatingQuestion();
		qns.add(q2);
		
		// Q3: create RatingQuestion using Scale
		Question q3 = createRatingQuestionUsingScale();
		qns.add(q3);
		
		// Q4: create SingleAnsMCQ
		Question q4 = createSingleAnsMCQ();
		qns.add(q4);
		
		// Q5: create Numeric Question
		Question q5 = createNumericQuestion();
		qns.add(q5);
		
		mgr.createQuestionnaire(qnaire);
		System.out.println("questionnaire created!!! ");
		
	}
	
	public Question createNumericQuestion(){
		QuestionFactory factory = QuestionFactory.getQuestionFactory(QuestionFactory.NUMERIC_QN);
		Question q = factory.createQuestion();
		q.setQuestionText("question 5: Numeric Question ");
		
		return q;
	}
	
	public Question createSingleAnsMCQ(){
		QuestionFactory factory = QuestionFactory.getQuestionFactory(QuestionFactory.SINGLE_ANS_MCQ);
		Question q = factory.createQuestion();
		q.setMandatory(Question.MANDATORY);
		q.setQuestionText("question 4: Single Ans MCQ");
		List<Option> options = q.getOptions();
		Option option1 = q.createOption();
		option1.setOptionText("option 1");
		Option option2 = q.createOption();
		option2.setOptionText("option 2");
		Option option3 = q.createOption();
		option3.setOptionText("option 3");
		Option option4 = q.createOption();
		option4.setOptionText("option 4");
		
		options.add(option1);
		options.add(option2);
		options.add(option3);
		options.add(option4);
		return q;
	}
	
	public Question createRatingQuestionUsingScale(){
		QuestionFactory factory2 = QuestionFactory.getQuestionFactory(QuestionFactory.RATING_QN);
		Question q3 = factory2.createQuestion();
		q3.setMandatory(Question.MANDATORY);
		q3.setQuestionText("question 3: rating question using scale");
		Scale scale = q3.createScale();
		qnaire.getScales().add(scale);
		
		scale.setScaleTitle("scale title");
		scale.setMaxValue(10f);
		scale.setMinValue(0f);
		ScaleOption scaleOption1 = new ScaleOption();
		scaleOption1.setOptionText("scale option 1");
		ScaleOption scaleOption2 = new ScaleOption();
		scaleOption2.setOptionText("scale option 2");
		ScaleOption scaleOption3 = new ScaleOption();
		scaleOption3.setOptionText("scale option 3");
		ScaleOption scaleOption4 = new ScaleOption();
		scaleOption4.setOptionText("scale option 4");
		List<ScaleOption> scaleOptions = scale.getScaleOptions();
		scaleOptions.add(scaleOption1);
		scaleOptions.add(scaleOption2);
		scaleOptions.add(scaleOption3);
		scaleOptions.add(scaleOption4);
		List<Option> ratingOptions = scale.getRatingOptions();
		q3.setOptions(ratingOptions);
		return q3;
	}
	
	public Question createMultipleAnsMCQ(){
		QuestionFactory factory1 = QuestionFactory.getQuestionFactory(QuestionFactory.MUL_ANS_MCQ);
		Question q1 = factory1.createQuestion();
		//if it is not a MCQ, this will not have any effect
		q1.setLayout(MCQQuestion.HORIZONTAL_LAYOUT);
		q1.setMandatory(Question.MANDATORY);
		q1.setQuestionText("question 1: MultipleAnsMCQ");
		
		List<Option> options = q1.getOptions();
		Option option1 = q1.createOption();
		option1.setOptionText("option 1");
		Option option2 = q1.createOption();
		option2.setOptionText("option 2");
		Option option3 = q1.createOption();
		option3.setOptionText("option 3");
		Option option4 = q1.createOption();
		option4.setOptionText("option 4");
		
		options.add(option1);
		options.add(option2);
		options.add(option3);
		options.add(option4);
		return q1;
	}
	
	public Question createRatingQuestion(){
		QuestionFactory factory2 = QuestionFactory.getQuestionFactory(QuestionFactory.RATING_QN);
		Question q2 = factory2.createQuestion();
		q2.setMandatory(Question.OPTIONAL);
		q2.setQuestionText("question 2 content");
		List<Option> options2 = q2.getOptions();
		Option r1 = q2.createOption();
		r1.setOptionText("rating option 1");
		r1.setRating(11.5f);
		Option r2 = q2.createOption();
		r2.setOptionText("rating option 2");
		r2.setRating(12.5f);
		Option r3 = q2.createOption();
		r3.setOptionText("rating option 3");
		r3.setRating(13.5f);
		Option r4 =q2.createOption();
		r4.setOptionText("rating option 4");
		r4.setRating(14.5f);
		options2.add(r1);
		options2.add(r2);
		options2.add(r3);
		options2.add(r4);
		
		return q2;
	}
	
	public static void main(String[] args) {
		try {
			TestCreateQuestionnaire client = new TestCreateQuestionnaire();
			client.createQuestionnaire();
			

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
